var app = getApp()
Page({
  data: {

    year: 0,
    month: 0,

    showMonth: 0,
    nowDay: 0,
    date: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
    dateArr: [],
    isToday: 0,
    isTodayWeek: false,
    todayIndex: 0,
    alr: [],
    months: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
    navbar: ['预约咨询', '召唤顾问'],
    currentTab: 0,
    reserveArray: [],
    timeStatus: [],
    hiddenTime: false,
    canReserve: true,
    needPay: 0,
    zhouInfo: {},
    panInfo:{},
    isactive1: false,
    isactive2: false,
    isactive3: false,
    isactive4: false,
    reserveInfoStr1: '',
    reserveInfoStr2: '',
    reserveInfoStr3: '',
    reserveInfoStr4: '',
    selectDay: 0,
    hasReserveArray: [],
    chooseIndex:0
  },
  onLoad: function () {
      console.log(app.globalData.myId)
    var now2 = new Date();
    var year2 = now2.getFullYear();
    var month2 = now2.getMonth();
    month2 = parseInt(month2) + 1
    var day2 = now2.getDate();
    if (month2 < 10) {
      month2 = "0" + month2
    }
    if (day2 < 10) {
      day2 = "0" + day2
    }
    this.setData({
      selectDay: day2
    })

    this.getInfo()
    this.getInfo1()
    this.getReserveInfo(null, year2 + "-" + month2)
    this.getDayInfo(year2 + "-" + month2 + '-' + day2, day2)
  },
  q: function () {
    wx.navigateBack({ changed: true })
  },
  qq: function () {
    wx.switchTab({

      url: '/pages/index/index',

    });
  },
  onReady: function () {
    const vm = this
    vm.setData({
      statusBarHeight: getApp().globalData.statusBarHeight,
      titleBarHeight: getApp().globalData.titleBarHeight
    })

  },
  getInfo: function () {
    var that = this
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/Consult/consult_index',
      data: {
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)

        if (res.data.status === 1) {
          that.setData({
            zhouInfo: res.data.data
          })

        } else {

        }

      },
      fail: function () {
        wx.hideLoading()
      }
    })
  },
  getInfo1: function () {
    var that = this
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/Consult/teacher_index',
      data: {
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        console.log("yuyue")
        if (res.data.status === 1) {
          that.setData({
            panInfo: res.data.data
          })

        } else {

        }

      },
     
      fail: function () {
        wx.hideLoading()
      }
    })
  },
  goSummonWrite: function (e) {
    var index = e.currentTarget.dataset.index
    var that = this
    console.log(that.data.panInfo[index].id)
    console.log("55")
    wx.navigateTo({
      url: '../summonWrite/summonWrite?img=' + that.data.panInfo[index].img + '&name=' + that.data.panInfo[index].name + '&nickname=' + that.data.panInfo[index].nickname + '&describe=' + that.data.panInfo[index].describe + '&id='+that.data.panInfo[index].id,

    })
  },
  getReserveInfo: function (e, e1) {
    console.log('e1=' + e1)
    var that = this
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/Consult/check_mon',
      data: {
        m_time: e1,
        myId: app.globalData.myId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log('getReserveInfo')
        console.log(res)
        wx.hideLoading()
        if (res.data.status === 1) {
          var a = [],
            b = [],
            c = []
          if (res.data.data.no.length != 0) {
            for (var i = 0; i < res.data.data.no.length; i++) {
              a.push(res.data.data.no[i])
            }

          }
          if (res.data.data.special.length != 0) {
            for (var i = 0; i < res.data.data.special.length; i++) {
              a.push(res.data.data.special[i])
            }
          }
          if (res.data.data.alr.length != 0) {
        
            var s = Object.keys(res.data.data.alr).length
            for (var i = 0; i < res.data.data.alr.length; i++) {
              c.push(res.data.data.alr[i])
            }
          }

          that.setData({
            reserveArray: a,
            alr: c
          })
          if (e == null) {
            let now = new Date();
            let year = now.getFullYear();
            let month = now.getMonth() + 1;
            let day = now.getDate();
            console.log("day=" + day)
            that.setData({
              nowDay: day,
              year: year,
              month: month,
              showMonth: month,
              isToday: '' + year + month + now.getDate()
            })
            that.dateInit(year, month);

          } else {
            that.setData({
              showMonth: parseInt(e.detail.value) + 1
            })
            that.dateInit(that.data.year, parseInt(e.detail.value) + 1)
          }

        } else {

        }
        that.data
      },
      fail: function () {

        wx.hideLoading()
      }
    })
  },
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },
  onUnload: function () {
    wx.switchTab({
      url: '../../index'

    })
  },

  getDayInfo: function (e1, e2) {
    var that = this
    console.log(e1)
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/Consult/check_day2',
      data: {
        d_time: e1,
        myId: app.globalData.myId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading()
        console.log('dddddd')
        console.log(res)

        if (res.data.status === 1) {
          that.setData({
            timeStatus: res.data.data
          })
          var dd = 0
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i] == 2) {
              dd = 1
            }
          }
          console.log('dd=' + dd)
          if (dd == 0) {
            wx.showToast({
              title: '无可预约时间段',
              icon: 'none'
            })
            that.setData({
              canReserve: false

            })
          } else {

            that.setData({
              canReserve: true,
              selectDay: e2
            })
          }
        } else {

        }

      },
      fail: function () {
        wx.hideLoading()
      }
    })
  },

  chooseTime: function (e) {

    var index = parseInt(e.currentTarget.dataset.index)

    var m = this.data.timeStatus
    var s = 't0'
    if (index == 0) {
      wx.showToast({
        title: '不可约',
        icon: 'none',

      })
    } else if (index == 1) {
      wx.showToast({
        title: '已预约',
        icon: 'none',

      })
    }

  },

  chooseDay: function (datas) {
    // console.log(index.currentTarget.dataset.data)
    console.log(this.data.dateArr)

    var item = datas.currentTarget.dataset.data
    var bb = item.dateNum
    var status = datas.currentTarget.dataset.status
    var index = datas.currentTarget.dataset.index
    if (item==null ) {
 
      return
    }
    // if (status == 1) {
    //   wx.showToast({
    //     title: '已预约',
    //     icon: 'none'
    //   })
    //   return
    // }
    if (status == 2) {
      wx.showToast({
        title: '不可约',
        icon: 'none'
      })
      return
    }
  
  

  this.setData({
    isactive1:item.isactive1,
    isactive2: item.isactive2,
    isactive3: item.isactive3,
    isactive4: item.isactive4,
    chooseIndex: index
  })


    this.data.isactive1 ? this.setData({
      status_css1: 'background-color:#d43b33;color:#fff'
    }) : this.setData({
      status_css1: 'background-color:#f8f8f8;'
    })

    this.data.isactive2 ? this.setData({
      status_css2: 'background-color:#d43b33;color:#fff'
    }) : this.setData({
      status_css2: 'background-color:#f8f8f8;'
    })


    this.data.isactive3 ? this.setData({
      status_css3: 'background-color:#d43b33;color:#fff'
    }) : this.setData({
      status_css3: 'background-color:#f8f8f8;'
    })


    this.data.isactive4 ? this.setData({
      status_css4: 'background-color:#d43b33;color:#fff'
    }) : this.setData({
      status_css4: 'background-color:#f8f8f8;'
    })


    this.setData({
      hiddenTime: false,
    })

    var nowTime = this.data.year + this.data.showMonth + item.dataNum

    if (this.data.showMonth == this.data.month){
      for (var i = 0; i < this.data.dateArr.length; i++) {
        if (this.data.dateArr[i].dateNum == item.dateNum) {
          this.setData({
            isToday: this.data.dateArr[i].isToday
          })
          break
        }
      }
    }
    
    var a = item.dateNum
    if (parseInt(a) < 10) {
      a = '0' + a
    }

    var b = parseInt(this.data.showMonth)
    if (b < 10) {
      b = '0' + b
    }
    var d = this.data.year + '-' + b + '-' + a
    wx.showLoading({
      title: '请稍候',
    })

    this.getDayInfo(d, a)
  },

  chooseMonth: function (e) {

    var a = parseInt(e.detail.value) + 1
    var month2 = ''
    if (a < 10) {
      month2 = "0" + a
    } else {
      month2 = a
    }
    console.log('month2=' + month2)
    this.getReserveInfo(e, this.data.year + '-' + month2)
    this.setData({
      needPay: 0
    })
    var now2 = new Date();
    var yearnow = now2.getFullYear();
    var monthnow = now2.getMonth();
    var daynow = now2.getDate();

    this.setData({
      hiddenTime: true,
      canReserve: false
    })

  },


  dateInit: function (setYear, setMonth) {
    var that = this
    console.log(setYear + "||" + setMonth)
    //全部时间的月份都是按0~11基准，显示月份才+1
    var dateArr = []; //需要遍历的日历数组数据
    var arrLen = 0; //dateArr的数组长度

    // this.data.month = setMonth ; //没有+1方便后面计算当月总天数
    var nextMonth = setMonth > 11 ? 1 : setMonth;

    var startWeek = new Date(setYear + '/' + setMonth + '/' + 1).getDay(); //目标月1号对应的星期
    var dayNums = new Date(setYear, nextMonth, 0).getDate(); //获取目标月有多少天

    var obj = {};
    var num = 0;
    var num2 = 0;
    var nextYear = '';

    if (setMonth > 11) {
      nextYear = that.data.year + 1;
      dayNums = new Date(nextYear, nextMonth, 0).getDate();
    }
    arrLen = startWeek + dayNums;

    var aa
    var bb = 0
    if (setMonth < 10) {
      setMonth = "0" + setMonth
    }
    for (let i = 0; i < arrLen; i++) {
      bb = 0
      if (i >= startWeek) {
        num = i - startWeek + 1;
        num2 = num;
        if (num < 10) {
          num = "0" + num
        }

        aa = setYear + "-" + setMonth + "-" + num

        if (that.data.alr.length != 0) {
          for (var s = 0; s < that.data.alr.length; s++) {
            if (that.data.alr[s] == aa) {// 自己约的
              bb = 1
              break
            }
          }
        }
        if (bb == 0) {
          if (that.data.reserveArray.length != 0) {// 有其他人预约的情况
            for (var m = 0; m < that.data.reserveArray.length; m++) {
              if (that.data.reserveArray[m] == aa) {// 当天已预约
                bb = 2 // 别人约了
                console.log('别人约的')
                break
              }
            }
          }
        }

        if (bb == 0) {
          if (this.data.month > parseInt(setMonth)) {// 当前日期>显示日期
            bb = 2
          }
        }
        if (bb == 0) {

          if (this.data.month == parseInt(setMonth) && parseInt(this.data.nowDay) > num2) {

            bb = 2
          }
        }
     

        obj = {
          isToday: '' + setYear + setMonth + num2,
          dateNum: num2,
          reservationState: bb,
          week: that.getMyDay(new Date(aa)),
          isactive1: false,
          isactive2: false,
          isactive3: false,
          isactive4: false,
        }
        if (obj.isToday == this.data.isToday) {
          this.setData({
            chooseIndex: i
          })
        }
      } else {
        obj = {};
      }
      dateArr[i] = obj;
    }
    this.setData({
      dateArr: dateArr
    })

    console.log(this.data.dateArr)
  },

  getMyDay: function (date) {
    var week;
    if (date.getDay() == 0) week = "7"
    if (date.getDay() == 1) week = "1"
    if (date.getDay() == 2) week = "2"
    if (date.getDay() == 3) week = "3"
    if (date.getDay() == 4) week = "4"
    if (date.getDay() == 5) week = "5"
    if (date.getDay() == 6) week = "6"
    return week;
  },


  sendInfo: function () {
    var a = this.data.year + '-' + this.data.showMonth + '-' 
    var array=this.data.dateArr
    var str=''
    console.log(array[10])
    for (var i = 0; i < array.length;i++){
      var temp=''
      // console.log(array[i])
      if (array[i].dateNum != undefined && array[i].dateNum!=''){
   
        if (array[i].dateNum<10){
          temp = '0' + array[i].dateNum
          }else{
          temp = array[i].dateNum
          }
        if (array[i].isactive1) {
          str = str+ a + temp + ' 14:00-15:00,'
        }
        if (array[i].isactive2) {
          str = str +a + temp + ' 15:00-16:00,'
        }
        if (array[i].isactive3) {
          str = str + a + temp + ' 16:00-17:00,'
        }
        if (array[i].isactive4) {
          str = str + a + temp + ' 17:00-18:00,'
        }

      }
   
    }
    if (str == '' || str == undefined) {
      wx.showToast({
        title: '请选择预约',
        icon: 'none'
      })
      return;
    }
    
    if (str.endsWith(',')) {
      str = str.substring(0, str.length - 1)
    }
    console.log(str)

    wx.navigateTo({
      url: '../makeWrite/makeWrite?time=' + str + '&id=' + this.data.zhouInfo.id + "&needPay=" + this.data.needPay,
    })



  },


  timeClick1: function (e) {
    let page = this;
    page.data.isactive1 = !this.data.dateArr[this.data.chooseIndex].isactive1
    page.data.isactive1 ? page.setData({
      status_css1: 'background-color:#d43b33;color:#fff'
    }) : page.setData({
      status_css1: 'background-color:#f8f8f8;'
    })

 
    if (page.data.isactive1) {
      this.setData({
        needPay: this.data.needPay + 2000,
      })
    } else {
      this.setData({
        needPay: this.data.needPay - 2000,
      })
    }
    var s=this.data.dateArr
    s[this.data.chooseIndex].isactive1 = page.data.isactive1 
    this.setData({
      dateArr:s,
    })


  },

  timeClick2: function (e) {
    let page = this;
    page.data.isactive2 = !this.data.dateArr[this.data.chooseIndex].isactive2
    page.data.isactive2 ? page.setData({
      status_css2: 'background-color:#d43b33;color:#fff'
    }) : page.setData({
      status_css2: 'background-color:#f8f8f8;'
    })


    if (page.data.isactive2) {
      this.setData({
        needPay: this.data.needPay + 2000,
      })
    } else {
      this.setData({
        needPay: this.data.needPay - 2000,
      })
    }
    var s = this.data.dateArr
    s[this.data.chooseIndex].isactive2 = page.data.isactive2
    this.setData({
      dateArr: s,
    })
  },

  timeClick3: function (e) {
    let page = this;
    page.data.isactive3 = !this.data.dateArr[this.data.chooseIndex].isactive3
    page.data.isactive3 ? page.setData({
      status_css3: 'background-color:#d43b33;color:#fff'
    }) : page.setData({
      status_css3: 'background-color:#f8f8f8;'
    })


    if (page.data.isactive3) {
      this.setData({
        needPay: this.data.needPay + 2000,
      })
    } else {
      this.setData({
        needPay: this.data.needPay - 2000,
      })
    }
    var s = this.data.dateArr
    s[this.data.chooseIndex].isactive3 = page.data.isactive3
    this.setData({
      dateArr: s,
    })
  },

  timeClick4: function (e) {
    let page = this;
    page.data.isactive4 = !this.data.dateArr[this.data.chooseIndex].isactive4
    page.data.isactive4 ? page.setData({
      status_css4: 'background-color:#d43b33;color:#fff'
    }) : page.setData({
      status_css4: 'background-color:#f8f8f8;'
    })


    if (page.data.isactive4) {
      this.setData({
        needPay: this.data.needPay + 2000,
      })
    } else {
      this.setData({
        needPay: this.data.needPay - 2000,
      })
    }
    var s = this.data.dateArr
    s[this.data.chooseIndex].isactive4 = page.data.isactive4
    this.setData({
      dateArr: s,
    })
  },

  lastMonth: function () {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
    let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  },
  nextMonth: function () {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
    let month = this.data.month > 11 ? 0 : this.data.month;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  },
  toRecordList: function () {
    wx.navigateTo({
      url: '../ConsultingRecord/ConsultingRecord',
    })
  }
})